package org.u2ml.stack.web;

import java.util.Collection;
import java.util.HashMap;

import org.mortbay.util.ajax.Continuation;

/**
 * List of Clients who wait for a responses 
 * @author Pascal
 *
 */
public class WaitingClients {
	
	private HashMap<String, Client> list;
	private static WaitingClients _this = null;
	
	public static WaitingClients getInstance() {
		if (_this==null)
			_this = new WaitingClients();
		return _this;
	}
	
	private WaitingClients() {
		this.list = new HashMap<String,Client>();
	}
	
	public synchronized boolean add(String sessionid, Client c) {
		list.put(sessionid, c);
		return true;
	}
	
	private synchronized Client remove(String sessionId) {
		return list.get(sessionId);
	}
	
	public synchronized boolean isEmpty() {
		return list.isEmpty();
	}
	
	public synchronized int size() {
		return list.size();
	}
	
	private static synchronized void wakeUp(String sessionId) {
		Continuation continuation = (Continuation) getInstance().remove(sessionId).getContinuation();
		continuation.resume();
	}
	
	public static synchronized void wakeUpSessions(Collection<String> sessions) {
		for(String session : sessions){
			if (getInstance().list.containsKey(session))
				wakeUp(session);
		}
	}
}
